package game.data.implement;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import game.data.interfaces.ItemData;
import game.data.interfaces.PlayerData;
import game.enums.ItemID;

public class gamePlayerData implements PlayerData {
	int mainLevelsDone;
	ItemData[] myInventory;
	
	public gamePlayerData() {
		myInventory = new ItemData[9];
	}

	@Override
	public ItemData[] getInventory() {
		return myInventory;
	}

	@Override
	public int addToInventory(ItemID value, int amount) {
		int done = 0;
		for (int i = 0; i < myInventory.length; i++) {
			if (myInventory[i].getItemID() == value && done == 0) {
				done = myInventory[i].getItemAmount()+amount;
				if (done > myInventory[i].getMaxStack()) {
					done = myInventory[i].getMaxStack();
				}
				myInventory[i].setItemAmount(done);
			}
		}
		if (done == 0) {
			for (int i = 0; i < myInventory.length; i++) {
				if (myInventory[i] == null && done == 0) {
					ItemData buffer = new gameItemData();
					buffer.setItemID(value);
					done = myInventory[i].getItemAmount()+amount;
					if (done > myInventory[i].getMaxStack()) {
						done = myInventory[i].getMaxStack();
					}
					buffer.setItemAmount(done);
					myInventory[i] = buffer;
				}
			}
		}
		return done;
	}

	@Override
	public boolean removeFromInventory(ItemID value, int amount) {
		boolean done = false;
		for (int i = 0; i < myInventory.length; i++) {
			if (myInventory[i].getItemID() == value 
					&& myInventory[i].getItemAmount() > amount 
					&& !done) {
				myInventory[i].setItemAmount(myInventory[i].getItemAmount()-amount);
				if (myInventory[i].getItemAmount() == 0) {
					myInventory[i] = null;
				}
				done = true;
			}
		}
		return done;
	}

	@Override
	public void clearInventory() {
		for (int i = 0; i < myInventory.length; i++) {
			myInventory[i] = null;
		}
	}

	@Override
	public void setLevelsDone(int value) {
		mainLevelsDone = value;
	}

	@Override
	public int getLevelsDone() {
		return mainLevelsDone;
	}

	@Override
	public void savePlayerData(int slot) {
		gamePlayerData buffer = new gamePlayerData();
		buffer.mainLevelsDone = mainLevelsDone;
		buffer.myInventory = myInventory.clone();
	    try
	    {
	       FileOutputStream fileOut = new FileOutputStream("saves/slot"+slot+".sav");
	       ObjectOutputStream out = new ObjectOutputStream(fileOut);
	       out.writeObject(buffer);
	       out.close();
	       fileOut.close();
	    }catch(IOException i)
	    {
	        i.printStackTrace();
	    }
	    buffer = null;
	}

	@Override
	public boolean loadPlayerData(int slot) {
		if (new File("saves/slot"+slot+".sav").isFile()) {
			return false;
		}
		gamePlayerData buffer = new gamePlayerData();
		try
		{
		   FileInputStream fileIn = new FileInputStream("saves/slot"+slot+".sav");
		   ObjectInputStream in = new ObjectInputStream(fileIn);
		   buffer = (gamePlayerData) in.readObject();
		   in.close();
		   fileIn.close();
		}catch(IOException i)
		{
		   i.printStackTrace();
		   return false;
		}catch(ClassNotFoundException c)
		{
		   c.printStackTrace();
		   return false;
		}
		for(int i=0;i<20;i++)
		{
			for(int j=0;j<20;j++)
			{
				myInventory = buffer.myInventory.clone();
				mainLevelsDone = buffer.mainLevelsDone;
			}
		}
		buffer = null;
		return true;
	}

}
